package com.dream.master.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dream.dao.master.ProductDAO;
import com.dream.dao.master.ProductPartDAO;
import com.dream.master.service.ProductCategoryService;
import com.dream.master.service.ProductService;
import com.dream.model.app.AppAbstractUser;
import com.dream.model.master.Product;
import com.dream.model.master.ProductCategory;
import com.dream.model.master.ProductPart;
import com.dream.stock.service.StockService;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.master.ProductPartVO;
import com.dream.vo.master.ProductVO;

@Service("productService")
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDAO productDAO;
	
	@Autowired
	private ProductPartDAO productPartDAO;
	
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@Autowired
	private StockService stockService;
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public PagingVO query(final ProductVO vo,final PagingVO page) {
		
		List<Long> cats = productCategoryService.getCats(vo.getFirstCat(), vo.getSecondCat());
		vo.setCategories(cats);
		PagingVO query = productDAO.query(vo, page);
		List<Product> li = (List<Product>) query.getDetails();
		if(CollectionUtils.isNotEmpty(li)){
			for(Product item : li){
				setProductCat(item);
			}
		}
		return query;
	}
	
	private void setProductCat(Product product){
		if(product != null){
			List<ProductCategory> cats = productCategoryService.getParent(product.getCategoryId());
			product.initCats(cats);
		}
		
	}

	@Override
	@Transactional
	public Product queryById(final String productId) {
		
		Product product = productDAO.selectById(productId);
		setProductCat(product);
		return product;
	}

	@Override
	@Transactional
	public void update(ProductVO vo, AppAbstractUser user) {
		
		Product product = new Product();
		if(StringUtils.isNotBlank(vo.getId())){
			product = productDAO.selectById(vo.getId());
			product.setUpdateTime(new Date());
			product.setUpdateUser(user.getId());
		}else{
			vo.setId(UUID.randomUUID().toString());

			product.setCreateTime(new Date());
			product.setCreateUser(user.getId());
		}
		vo.populate(product);
		
		productDAO.saveOrUpdate(product);
	}

	@Override
	@Transactional
	public void delete(String id, AppAbstractUser user) {
		
		Product product = productDAO.selectById(id);
		product.setDeleted(true);
		productDAO.saveOrUpdate(product);
		
	}
	
	
	@Override
	@Transactional
	public PagingVO queryPart(ProductPartVO vo, PagingVO page) {
		
		PagingVO query = productPartDAO.query(vo, page);
		return query;
	}

	@Override
	@Transactional
	public ProductPart queryPartById(String partId) {
		
		return productPartDAO.selectById(partId);
	}

	@Override
	@Transactional
	public void updatePart(ProductPartVO vo, AppAbstractUser user) {
		
		if(StringUtils.isBlank(vo.getId())){return; }
		
		ProductPart part =  productPartDAO.getById(vo.getId());
		if(part != null){
			part.setDeleted(false);
			part.setUpdateTime(new Date());
			part.setUpdateUser(user.getId());
		}else{
			part = new ProductPart();
			part.setDeleted(false);
			part.setCreateTime(new Date());
			part.setCreateUser(user.getId());
		}
		vo.populate(part);
		
		productPartDAO.saveOrUpdate(part);
		String partId = part.getId();
		stockService.initStockData(partId);
		
	}

	@Override
	@Transactional
	public void deletePart(String id, AppAbstractUser user) {
		
		ProductPart part =  productPartDAO.getById(id);
		part.setDeleted(true);
		productPartDAO.saveOrUpdate(part);
		
	}

	@Override
	@Transactional
	public Product search(String name, Long cat) {
		return productDAO.search(name, cat);
	}
	
	
	

}
